package com.efast.dmom.mgmt.service.mes.report.impl;

import java.text.NumberFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.commponent.query.service.impl.QueryServiceImpl;
import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.portal.bean.query.QueryBean;
import com.efast.cafe.portal.bean.query.QueryConditionBean;
import com.efast.cafe.portal.bean.query.QueryOpBean;
import com.efast.dmom.mgmt.bean.mes.report.VehicleQualityIndexBean;
import com.efast.dmom.mgmt.dao.mes.report.ReportVehicleRetrospectDao;
import com.efast.dmom.mgmt.utils.Assist;

import jodd.util.StringUtil;

/**
 * 重写整车单台缺陷项次通用查询方法
 * @author Administrator
 *
 */
@Service("apiindex533-511")
public class VehicleQualityIndexDPVServiceImpl extends QueryServiceImpl{

	@Autowired
	private ReportVehicleRetrospectDao reportVehicleRetrospectDao; 
	
	@Override
	public Object queryList(QueryBean querys) {
		QueryPageBase result = new QueryPageBase();
		QueryConditionBean queryConditionBean = querys.getQueryConditionBean();
		Assist assist = new Assist();
		if(queryConditionBean!=null) {
			if(queryConditionBean.getStart() == null || queryConditionBean.getLength() == null) {
				queryConditionBean.setStart(0);
				queryConditionBean.setLength(querys.getTemplate().getPagenation());
			}
			assist.setStartRow(queryConditionBean.getStart());
			assist.setRowSize(queryConditionBean.getLength());
			List<QueryOpBean> queryOpBeans = queryConditionBean.getQueryOpBeans();
			if(queryOpBeans != null && queryOpBeans.size() > 0) {
				for (QueryOpBean data : queryOpBeans) {
					   if(StringUtil.isNotBlank(data.getKey())&& StringUtil.isNotBlank(data.getValue())) {
						   if (data.getOpName().equals("包含")) {
								assist.setRequires(Assist.andLike(data.getKey(), "%" + data.getValue() + "%"));
							}
							if (data.getOpName().equals("等于")) {
								assist.setRequires(Assist.andEq(data.getKey(), data.getValue()));
							}
							if (data.getOpName().equals("介于(包含)")) {
								String[] split = data.getValue().split("~");
								String beginTime = "";
								String endTime = "";
								if(split!=null &&split.length>1) {
									 beginTime = split[0];
									 endTime = split[1];
								}
								assist.setRequires(Assist.andGte(data.getKey(), beginTime));
								assist.setRequires(Assist.andLte(data.getKey(), endTime));
							}
					   }
					}
				}
			//获取每日、每周、每月的缺陷数总和，以及通过所有质量门检验的车辆总数
			
			NumberFormat numberFormat = NumberFormat.getInstance();
			numberFormat.setMaximumFractionDigits(2);
			List<VehicleQualityIndexBean> vehicleQualityIndexBean = reportVehicleRetrospectDao.selectVehicleDPVInformation(assist);
			int countSize = reportVehicleRetrospectDao.selectVehicleDPVInformationCount(assist);			
			if (vehicleQualityIndexBean != null && vehicleQualityIndexBean.size() > 0) {
				for (VehicleQualityIndexBean data:vehicleQualityIndexBean) {
					data.setDayDPV(numberFormat.format(data.getDayDPVData()));
					data.setWeekDPV(numberFormat.format(data.getWeekDPVData()));
					data.setMonthDPV(numberFormat.format(data.getMonthDPVData()));
				}
			}
			result.setResultList(vehicleQualityIndexBean);
			result.setTotalRows(countSize);
			}
		return result;
   }
	
}
